package com.tianma315.admin.check.controller;


import java.util.Arrays;
import java.util.Calendar;

import com.tianma315.admin.check.vo.CheckRecordAndArrtVO;
import com.tianma315.admin.check.vo.CheckRecordArrtVO;
import com.tianma315.admin.common.base.BaseController;
import com.tianma315.admin.common.utils.Result;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.tianma315.admin.check.domain.CheckRecordDO;
import com.tianma315.admin.check.service.CheckRecordService;

/**
 * 
 * <pre>
 * 检测记录
 * </pre>
 * <small> 2019-05-28 16:30:57 | Aron</small>
 */
@Controller
@RequestMapping("/check/checkRecord")
public class CheckRecordController extends BaseController{
	@Autowired
	private CheckRecordService checkRecordService;
	
	@GetMapping()
	@RequiresPermissions("check:checkRecord:checkRecord")
	String CheckRecord(){
	    return "check/checkRecord/checkRecord";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("check:checkRecord:checkRecord")
	public Result<Page<CheckRecordDO>> list(Integer pageNumber, Integer pageSize, CheckRecordDO checkRecordDTO){
		// 查询列表数据
        Page<CheckRecordDO> page = new Page<>(pageNumber, pageSize);
        
        Wrapper<CheckRecordDO> wrapper = new EntityWrapper<CheckRecordDO>(checkRecordDTO);
        page = checkRecordService.selectPage(page, wrapper);
        int total = checkRecordService.selectCount(wrapper);
        page.setTotal(total);
        return Result.ok(page);
	}
	
	@GetMapping("/addCheckRecordAndArrt/{id}/{checkTemplateId}/{materialName}")
	String addCheckRecordAndArrt(@PathVariable("id")Integer id,@PathVariable("checkTemplateId")Integer checkTemplateId,@PathVariable("materialName")String materialName, Model model){
		model.addAttribute("purchaseId",id);
		model.addAttribute("productName",materialName);
		model.addAttribute("userName",getUsername());
		model.addAttribute("checkTemplateId",checkTemplateId);
	    return "check/checkRecord/addCheckRecordAndArrt";
	}

	@GetMapping("/add")
	@RequiresPermissions("check:checkRecord:add")
	String add(){
		return "check/checkRecord/add";
	}

	@GetMapping("/edit/{checkRecordId}")
	@RequiresPermissions("check:checkRecord:edit")
	String edit(@PathVariable("checkRecordId") Integer checkRecordId,Model model){
		CheckRecordDO checkRecord = checkRecordService.selectById(checkRecordId);
		model.addAttribute("checkRecord", checkRecord);
	    return "check/checkRecord/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
//	@RequiresPermissions("check:checkRecord:add")
	public Result<String> save( CheckRecordAndArrtVO checkRecordAndArrtVO){
		checkRecordAndArrtVO.setCreateBy(getUserId());
		checkRecordAndArrtVO.setCreateDate(Calendar.getInstance().getTime());
		System.out.println(checkRecordAndArrtVO);
//		if(checkRecordService.insert(checkRecord)){
//			return Result.ok();

		checkRecordService.addCheckRecordAndArrt(checkRecordAndArrtVO);
		return Result.ok();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
//	@RequiresPermissions("check:checkRecord:edit")
	public Result<String>  update( CheckRecordArrtVO checkRecordAndArrtVO){
		System.out.println(checkRecordAndArrtVO);
		checkRecordService.updateCheckRecordArrtVO(checkRecordAndArrtVO);
		return Result.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("check:checkRecord:remove")
	public Result<String>  remove( Integer checkRecordId){
		if(checkRecordService.deleteById(checkRecordId)){
            return Result.ok();
		}
		return Result.fail();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("check:checkRecord:batchRemove")
	public Result<String>  remove(@RequestParam("ids[]") Integer[] checkRecordIds){
		checkRecordService.deleteBatchIds(Arrays.asList(checkRecordIds));
		return Result.ok();
	}


	/**
	 * 获取检测记录
	 */
	@GetMapping("/getCheckRecordArrtVO/{purchaseId}")
	String getCheckRecordArrtVO(@PathVariable("purchaseId")Integer purchaseId,Model model){
		CheckRecordArrtVO checkRecordAndArrtVO = checkRecordService.getCheckRecordArrtVO(purchaseId);
		model.addAttribute("checkRecordAndArrtVO",checkRecordAndArrtVO);
		return "check/checkRecord/checkRecordArrt";
	}


}
